package components
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.List;
	import mx.events.ListEvent;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class MyResources{
		private var panel:SuperPanel = new SuperPanel();
		private var lista:List = new List();
		private var tool:Tool = new Tool();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var label1:Label = new Label();
		private var newResourceBtn:Button = new Button();
		private var deleteResourceBtn:Button = new Button();
		private var editResourceBtn:Button = new Button();
		private var newResource:NewResource;
		private var editResource:EditResource;
		
		public function MyResources(p:SuperPanel){
			panel = p;
			lista.x = 10;
			lista.y = 45;
			lista.height = panel.height-100;
	 		lista.width=panel.width-30;
	 		lista.editable = false;
	 		lista.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDoubleClick);
			newResourceBtn.label = "DODAJ";
			newResourceBtn.x = panel.width-90;
			newResourceBtn.y = panel.height-45;
			editResourceBtn.label = "EDITUJ";
			editResourceBtn.x = panel.width-165;
			editResourceBtn.y = panel.height-45;
			deleteResourceBtn.label = "IZBRISI";
			deleteResourceBtn.x = panel.width-245;
			deleteResourceBtn.y = panel.height-45;
			panel.addChild(lista);
			panel.addChild(newResourceBtn);
			panel.addChild(editResourceBtn);
			panel.addChild(deleteResourceBtn);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			newResourceBtn.addEventListener(MouseEvent.CLICK, onNewResourceBtnClick);
			editResourceBtn.addEventListener(MouseEvent.CLICK, onEditResourceBtnClick);
			deleteResourceBtn.addEventListener(MouseEvent.CLICK, onDeleteResourceBtnClick);
     		service.sql = {type:"SELECT", a1:"r.id_resurs", a2:"r.url", a3:"t.tip", a4:"r.sifra", a5:"r.naziv", a6:"r.opis", a7:"r.datum", a8:"r.vrijeme", a9:"t.id_tip", a10:"t.program", from:"resursi r, tipovi t", where: "t.id_tip=r.tip AND r.user="+panel.parentApplication.username };
	 		service.addEventListener(service.MYSQL_RESULT, onResourcesResult);
	 		service.startService();
		}
		
		public function onEditResourceBtnClick(e:MouseEvent):void{
			if(lista.selectedIndices.length>0){
				if(!panel.parentApplication.checkExist("Edituj resurs")){
					var s:SuperPanel;
					panel.parentApplication.panels.push(s = tool.createPanel("Edituj resurs", 0, 300, 250, 400, 100, true, false, "EditujResurs"));
					panel.parentApplication.addChild(s);
					s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
					s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
					editResource = new EditResource(s);
					editResource.show(lista.selectedItem.@id_resurs, lista.selectedItem.@tip, lista.selectedItem.@url, lista.selectedItem.@sifra, lista.selectedItem.@naziv, lista.selectedItem.@opis);
				}
			} else tool.createAlert("GRESKA", "Niste selektovali resurs!");
		}
		
		public function onDoubleClick(e:ListEvent):void{
				if(!panel.parentApplication.checkExist("Resurs("+lista.dataProvider[e.rowIndex].@id_resurs+")")){
					var s:SuperPanel;
					panel.parentApplication.panels.push(s = tool.createPanel("Resurs("+lista.dataProvider[e.rowIndex].@id_resurs+")", 0, 330, 280, 300, 150, true, false, "Resurs("+lista.dataProvider[e.rowIndex].@id_resurs+")"));
					panel.parentApplication.addChild(s);
					s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
					s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
					var resurs:Resource = new Resource(s);
										
					resurs.show(lista.dataProvider[e.rowIndex].@id_resurs, lista.dataProvider[e.rowIndex].@url, lista.dataProvider[e.rowIndex].@tip, panel.parentApplication.username, panel.parentApplication.user, lista.dataProvider[e.rowIndex].@program, lista.dataProvider[e.rowIndex].@sifra, lista.dataProvider[e.rowIndex].@naziv, lista.dataProvider[e.rowIndex].@opis, lista.dataProvider[e.rowIndex].@datum, lista.dataProvider[e.rowIndex].@vrijeme);
					if(lista.dataProvider[e.rowIndex].@sifra!="") {
						resurs.sifrirano = true;				
					} else resurs.sifrirano = false;
				}
		}
			
		public function onDeleteResourceBtnClick(e:MouseEvent):void{
				if(lista.selectedIndices.length>0){
					service1.sql = {type:"DELETE", from:"resursi", where:"id_resurs="+lista.selectedItem.@id_resurs};
		 			service1.addEventListener(service1.MYSQL_RESULT, onDeleteResult);
			 		service1.startService();
			 	} else tool.createAlert("GRESKA", "Niste selektovali resurs!");
		}
		
		public function onDeleteResult(e:Event):void{
			if(lista.selectedIndices.length>0){
				tool.createAlert("OK", "Resurs je uspjesno izbrisan!");
				if(lista.selectedItem.@tip!=1 && lista.selectedItem.@tip!=2 && lista.selectedItem.@tip!=3){
					service2.sql = {type:"DELETEMATERIJAL", file:lista.selectedItem.@url };
	 				service2.addEventListener(service1.MYSQL_RESULT, onDeleteMaterijalResult);
					service2.startService();
				}
			} else tool.createAlert("GRESKA", "Niste selektovali resurs!");
			lista.dataProvider.removeItemAt(lista.selectedIndex);
		}
		
		public function onDeleteMaterijalResult(e:Event):void{
			//
		}
		
		public function onNewResourceBtnClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("Novi resurs")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("Novi resurs", 0, 300, 250, 400, 100, true, false, "NoviResurs"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				newResource = new NewResource(s);
			}
		}
		
		public function onResourcesResult(e:Event):void{
			var xmlList:XMLList = service.xml.children();
			var xmlPom:String = "<root naziv='Resursi' tip='-1'>";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<resurs program='"+xmlList[i].program+"' id_resurs='"+xmlList[i].id_resurs+"' url='"+xmlList[i].url+"' tip='"+xmlList[i].tip+"' sifra='"+xmlList[i].sifra+"' naziv='"+xmlList[i].naziv+"' naziv1='"+xmlList[i].naziv+"  ("+xmlList[i].id_resurs+")"+"' opis='"+xmlList[i].opis+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"' />";
			}
			xmlPom+="</root>";
			var xml:XML=new XML(xmlPom);
			lista.dataProvider = xml.children();
			lista.labelField = "@naziv1";
		}
		
		public function onResize(e:ResizeEvent):void{
	 		lista.width=panel.width-30;
			label1.width = panel.width-30;
			lista.height = panel.height-100;
			newResourceBtn.x = panel.width-90;
			newResourceBtn.y = panel.height-45;
		}
	}
}